package com.bucks.controller;

import com.bucks.controller.request.NewOrderRequest;
import com.bucks.entity.Coffee;
import com.bucks.entity.CoffeeOrder;
import com.bucks.service.CoffeeOrderService;
import com.bucks.service.CoffeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**

 * @date 2019-11-15
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class CoffeeOrderController {

    @Autowired
    private CoffeeOrderService orderService;

    @Autowired
    private CoffeeService coffeeService;

    @GetMapping("/id")
    public CoffeeOrder getOrder(@PathVariable("id") Long id){
        CoffeeOrder order = orderService.get(id);
        log.info("Get Order: {}",order);
        return order;
    }

    @PostMapping(path = "/", consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseStatus(HttpStatus.CREATED)
    public CoffeeOrder create(@RequestBody NewOrderRequest newOrderRequest){
        log.info("Receive new Order {}", newOrderRequest);
        Coffee[] coffees = coffeeService.getCoffeeByName(newOrderRequest.getItems())
                .toArray(new Coffee[]{});
        return orderService.createOrder(newOrderRequest.getCustomer(),coffees);
    }











}
